import time
from datetime import datetime
import asyncio

from interview_voice_project.__001__langgraph_more_node.langgraph_agent import interview_voice_analyse
from interview_voice_project.common import get_project_root
from interview_voice_project.common.time_utils import get_datetime_str_from_datetime
from interview_voice_project.__002__db_helper_parse.db_helper import my_db_helper


def run_langgraph():
    while True:
        all_interview_records = my_db_helper.get_all_interview_records({"processing_status": 0})
        if all_interview_records:
            interview_record = all_interview_records[0]
            print(interview_record)
            if not interview_record.get('recording_url'):
                my_db_helper.update_interview_record(interview_record['id'], {"processing_tips": "录音缺失，无法处理", "processing_status": 2})
                time.sleep(2)
                continue
            # 更新面试记录为处理中
            my_db_helper.update_interview_record(interview_record['id'], {"processing_status": 1, "process_start_time": datetime.now()})
            my_db_helper.delete_analysis_details_by_record_id(interview_record['id'])
            # "input_audio_path": file_location, "name": name, "company": company, "subject": subject,
            # "interview_date_str": interview_date_str
            interview_info_dict = {
                "name": interview_record["name"],
                "company": interview_record["company_name"],
                "subject": interview_record["subject"],
                "interview_date_str": get_datetime_str_from_datetime(interview_record['interview_time'])
            }
            try:
                asyncio.run(interview_voice_analyse(get_project_root.joinpath(interview_record['recording_url']),
                                                    interview_record['id'], interview_info_dict))
                # 更新面试记录为处理完成
                my_db_helper.update_interview_record(interview_record['id'], {"processing_status": 2, "process_end_time": datetime.now()})
            except Exception as e:
                my_db_helper.update_interview_record(interview_record['id'], {"processing_status": 3, "processing_tips": str(e), "process_end_time": datetime.now()})
        time.sleep(30)


run_langgraph()
